package com.hxkj.service.impl;

import com.github.pagehelper.PageInfo;
import com.hxkj.client.service.DictItemServiceClient;
import com.hxkj.domain.enums.ArticleExceptionEnum;
import com.hxkj.domain.enums.DictItemExceptionEnum;
import com.hxkj.domain.po.DictItem;
import com.hxkj.service.DictItemService;
import com.platform.common.domain.exception.CustomException;
import com.platform.common.domain.exception.CustomMessageException;
import com.platform.common.domain.result.ParameterDataStruct;
import com.platform.common.domain.result.ResultDataStruct;
import com.platform.common.domain.result.ResultMessageStruct;
import com.platform.common.domain.to.PageTo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class DictItemServiceImpl implements DictItemService{
    @Autowired
    private DictItemServiceClient dictItemServiceClient;



    @Override
    public PageInfo getDiceItemList(DictItem dictItem, PageTo pageParam) throws Exception {
        if (pageParam.getPageNum() == null || pageParam.getPageSize() == null) {
            pageParam.setPageNum(1);
            pageParam.setPageSize(20);
        }
        if (dictItem.getDictTypeCode() == null) {
            throw new CustomException(DictItemExceptionEnum.DICTTYP_ITEMVALUE_NULL);
        }
        ParameterDataStruct<DictItem> parameterDataStruct = new ParameterDataStruct<>();
        parameterDataStruct.setData(dictItem);
        parameterDataStruct.setPageTo(pageParam);
        ResultDataStruct<PageInfo>  resultDataStruct = dictItemServiceClient.getlist(parameterDataStruct);
        if(resultDataStruct.getCode() == 200){
            return resultDataStruct.getData();
        }
        throw new CustomMessageException(resultDataStruct.getCode(),resultDataStruct.getMessage());
    }

    @Override
    public DictItem getDiceItemFromCache(DictItem diceItem) throws Exception {
        ResultDataStruct<DictItem> result = new ResultDataStruct();
        if (diceItem.getDictItemId() == null) {
            throw new CustomException(DictItemExceptionEnum.DICTITEM_NOT_ID);
        }
        try {
            // 查询一条二级 字典
            result = dictItemServiceClient.get(diceItem);
            if (result.getCode() != null) {
                throw new CustomException(ArticleExceptionEnum.getEnumByCode(result.getCode()));
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
        return result.getData();
    }

    @Override
    public void updateDiceItem(DictItem diceItem) throws Exception {
        ResultMessageStruct   result = new ResultMessageStruct();
        if (diceItem.getDictItemId() == null) {
            throw new CustomException(DictItemExceptionEnum.DICTITEM_NOT_ID);
        }
        if (diceItem.getDictTypeCode() == null) {
            throw new CustomException(DictItemExceptionEnum.DICTTYP_ITEMVALUE_NULL);
        }
        if (diceItem.getDictItemName() == null) {
            throw new CustomException(DictItemExceptionEnum.DICTITEM_NOT_NAME);
        }
        if (diceItem.getDictItemCode() == null) {
            throw new CustomException(DictItemExceptionEnum.DICTTYP_ITEMVALUE_NULL);
        }
        if (diceItem.getDelTag() == null) {
            throw new CustomException(DictItemExceptionEnum.DICTTYP_DEL_TAG_NULL);
        }
        diceItem.setUpdateTime(System.currentTimeMillis());
        try {
            //修改字典
            result = dictItemServiceClient.updateDictItem(diceItem);
            if (result.getCode() != null) {
                throw new CustomException(DictItemExceptionEnum.getEnumByCode(result.getCode()));
            }
        }catch (Exception e){
            e.printStackTrace();
            throw e;
        }
    }


    @Override
    public void deleteDiceItem(DictItem diceItem) throws Exception {
        ResultMessageStruct   result = new ResultMessageStruct();
        if (diceItem.getDictItemId() == null) {
            throw new CustomException(DictItemExceptionEnum.DICTITEM_NOT_ID);
        }
        try {
            //删除二级字典
            result = dictItemServiceClient.deleteDictItem(diceItem);
            if (result.getCode() != null) {
                throw new CustomException(DictItemExceptionEnum.getEnumByCode(result.getCode()));
            }
        }catch (Exception e){
            e.printStackTrace();
            throw e;
        }
    }

    @Override
    public void saveDiceItem(DictItem diceItem) throws Exception {
        ResultMessageStruct   result = new ResultMessageStruct();
        if (diceItem.getDictTypeCode() == null) {
            throw new CustomException(DictItemExceptionEnum.DICTTYP_ITEMVALUE_NULL);
        }
        if (diceItem.getDictItemName() == null) {
            throw new CustomException(DictItemExceptionEnum.DICTITEM_NOT_NAME);
        }
        if (diceItem.getDictItemCode() == null) {
            throw new CustomException(DictItemExceptionEnum.DICTTYP_ITEMVALUE_NULL);
        }
        if (diceItem.getDelTag() == null) {
            throw new CustomException(DictItemExceptionEnum.DICTTYP_DEL_TAG_NULL);
        }
        diceItem.setCreateTime(System.currentTimeMillis());
        try {
            //新增二级字典
            result = dictItemServiceClient.saveDictItem(diceItem);
            if (result.getCode() != null) {
                throw new CustomException(DictItemExceptionEnum.getEnumByCode(result.getCode()));
            }
        }catch (Exception e){
            e.printStackTrace();
            throw e;
        }
    }

    @Override
    public void modifyDictItem(DictItem dictItem) throws Exception {
        ResultMessageStruct   result = new ResultMessageStruct();
        if (dictItem.getDictItemId() == null) {
            throw new CustomException(DictItemExceptionEnum.DICTITEM_NOT_ID);
        }
        if (dictItem.getDelTag() == null) {
            throw new CustomException(DictItemExceptionEnum.DICTTYP_DEL_TAG_NULL);
        }
        try {
            //禁用二级字典
            result = dictItemServiceClient.modifyDictItem(dictItem);
            if (result.getCode() != null) {
                throw new CustomException(DictItemExceptionEnum.getEnumByCode(result.getCode()));
            }
        }catch (Exception e){
            e.printStackTrace();
            throw e;
        }
    }

    @Override
    public List<DictItem> comboBox(DictItem dictItem) throws Exception {
        ResultDataStruct< List<DictItem>> result = new ResultDataStruct();
        if (dictItem.getDictTypeCode() == null) {
            throw new CustomException(DictItemExceptionEnum.DICTTYP_ITEMVALUE_NULL);
        }
        try {
            // 获取一个字典下拉框  二 级（子类）
            result = dictItemServiceClient.comboBox(dictItem);
            if (result.getCode() != null) {
                throw new CustomException(DictItemExceptionEnum.getEnumByCode(result.getCode()));
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
        return result.getData();
    }

}
